import { createRouter, createWebHashHistory } from "vue-router";
import $common from "@/utils/common";

const routes = [
  {
    path: "/",
    component: () => import("../views/layout"),
    children: [
      {
        path: "",
        component: () => import("../views/homePage"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("../views/loginPage"),
  },
  {
    path: "/test",
    component: () => import("../views/testPage"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  let noLoginPathList = ["/test", "/login"];
  if (noLoginPathList.includes(to.path)) {
    next();
  } else {
    let userData = $common.getlocalStorage("userData");
    console.log(userData);
    if (userData) {
      next();
    } else {
      next("/login");
    }
  }
});

export default router;
